<!-- 旅游项目详情 -->
<template>
    <div>
        <a-page-header
            style="border: 1px solid rgb(235, 237, 240);background:#fff"
            :title="itemTypeName+'-'+travelName+'详情信息'"
            @back="() => $router.go(-1)"
        >
            <div class='table-page-search-wrapper'>
                <a-form layout='inline'>
                <a-row :gutter='48'>
                        <a-col :md='16' :sm='24'>
                        </a-col>
                        <a-col :md='8' :sm='24'>
                            <a-form-item >
                                <a-button type="primary" style="margin-right: 20px" @click="getData()">查询</a-button>
                                <a-button type="" icon="plus" @click="add()">添加</a-button>
                            </a-form-item>
                        </a-col>
                    </a-row>
                </a-form>
            </div>  

            <a-table rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ pageSize: 10, total: total ,showSizeChanger: true,pageSize: size,}" size="small">
                <div slot="name" slot-scope="name">
                    <ellipsis :length="18" tooltip>{{ name }}</ellipsis>
                </div>
                <div slot="itemDate" slot-scope="text,record">
                    <a-button v-if="hasAuth('updateTravelItemDate')" type="link" @click="showItemDate(record)">配置</a-button>
                    <div v-else>-</div>
                </div>
                <div slot="origin" slot-scope="origin">
                    <a-tag v-if="origin" :color="colors[0]">{{origin}}</a-tag>
                    <div v-else>-</div>
                </div>
                <div slot="destination" slot-scope="destination">
                    <a-tag v-if="destination" :color="colors[1]">{{destination}}</a-tag>
                    <div v-else>-</div>
                </div>
                <div slot="number" slot-scope="number">
                    <ellipsis v-if="number" :length="18" tooltip>{{ number }}</ellipsis>
                    <div v-else>-</div>
                </div>
                <div slot="description" slot-scope="description">
                    <ellipsis :length="80" tooltip>{{ description }}</ellipsis>
                </div>
                <div slot="operation" slot-scope="text,record">
                    <a-button type="link" @click="update(record)">修改</a-button>
                    <a-popconfirm
                        title="是否确认删除当前配置?"
                        ok-text="Yes"
                        cancel-text="No"
                        @confirm="del(record)"
                    >
                        <a-button class="errorColor" type="link">删除</a-button>
                    </a-popconfirm>
                </div>
            </a-table>
        </a-page-header>

        <a-modal
            title="添加、修改旅游项目套餐信息"
            :visible="visible"
            @ok="handleOk"
            :confirm-loading="confirmLoading"
            @cancel="visible = false"
            >
            <div>
                <a-form :label-col="{ span: 8 }" :wrapper-col="{ span: 14 }">
                    <a-form-item label="套餐名称">
                        <a-input v-model="item.name" placeholder="请输入项目套餐名称" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="包含人数">
                        <a-input v-model="item.number" placeholder="请输入项目套餐包含人数" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="套餐价格">
                        <a-input v-model="item.amount" placeholder="请输入项目套餐价格" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="出发地">
                        <a-select v-model="item.origin" placeholder="请选择" allowClear>
                            <a-select-option v-for="(item,i) in cityList" :key="i" :value="item.city">{{item.province}}-{{item.city}}</a-select-option>
                        </a-select>
                    </a-form-item>
                    <a-form-item label="目的地">
                        <a-select v-model="item.destination" placeholder="请选择" allowClear>
                            <a-select-option v-for="(item,i) in cityList" :key="i" :value="item.city">{{item.province}}-{{item.city}}</a-select-option>
                        </a-select>
                    </a-form-item>
                    <a-form-item label="套餐介绍">
                        <a-input v-model="item.description" type="textarea" placeholder="请输入套餐介绍" allow-clear></a-input>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>

        <a-drawer width="900" :title="`${item.name}-日期配置`" placement="right" :closable="true" :visible="planDateTrueFalseBy" @close="onClose">
            <a-calendar :disabledDate="disabledDate" @select="dateEvent">
                <div slot="dateCellRender" slot-scope="value" class="events">
                    <div v-for="(item,i) in showPlanDatePrice(value)" :key="i">
                        <a-tag v-if="item && i < 2" :color="colors[i%13]">{{i == 0 ? '价格：' : '持续天数：'}}{{item}}</a-tag>
                    </div>
                </div>
            </a-calendar>
        </a-drawer>

        <!-- 设置套餐日期价格 -->
        <a-modal
            title="添加、修改旅游项目套餐信息"
            :visible="planDatePriceVisible"
            @ok="planDatePriceHandleOk"
            :confirm-loading="planDatePriceConfirmLoading"
            @cancel="planDatePriceVisible = false"
            >
            <div>
                <a-form :label-col="{ span: 8 }" :wrapper-col="{ span: 14 }">
                    <a-form-item label="套餐名称">
                        <a-input v-model="item.name" :disabled="true" placeholder="请输入项目套餐名称" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="套餐日期">
                        <a-input v-model="planDate" :disabled="true" placeholder="请输入项目套餐日期" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="套餐价格">
                        <a-input v-model="planDateAmount" placeholder="请输入项目套餐价格" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="持续天数">
                        <a-input v-model="travelDay" placeholder="请输入项目套餐持续天数" allow-clear></a-input>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>
    </div>
</template>

<script>
import { queryTravelItemPlan , updateTravelItemPlan , queryTravelCity , updateTravelItemDate , queryTravelItemDate } from '@/api/travel';
import { uploadFile , queryAreaInfo } from '@/api/brand';
import { columns } from './table';
import { Ellipsis } from '@/components';
import { getBase64 } from '@/utils/util';
import moment from 'moment';
export default {
    components: {
        Ellipsis,
    },
    data () {
        return {
            travelId: this.$route.query.travelId,
            travelName: this.$route.query.travelName,
            itemTypeName: this.$route.query.itemTypeName,
            dataList: [],
            total: 0,
            isload: false,
            page: 1,
            size: 10,
            columns,
            item: {
                'id': '',
                'name': '', // 名称
                'description': '', // 套餐内容
                'amount': '', // 价格
                'origin': '', // 出发地
                'destination': '', // 目的地
                'number': '', // 出行人数
            },
            colors: ['blue','purple','cyan','green','orange','red','pink','brown','indigo','lime','purple','teal','yellow','amber'],
            confirmLoading: false,
            isAdd: false,
            visible: false,
            cityList: [],
            planDateTrueFalseBy: false,
            dateList: [moment('2024-03-03')],
            planDateAmount: '',
            travelDay: '',
            planDate: '',
            planDatePriceConfirmLoading: false,
            planDatePriceVisible: false,
            planDateList: [],
        };
    },
    async created(){
        try {
            this.cityList = await this._queryCityList();
            this.getData()
        } catch (error) {
            this.$notification['error']({
                message: '提示',
                description: error,
                duration: 8,
            })
        }
    },
    methods: {
        // 获取旅游项目套餐信息
        getData(){
            let _this = this
            _this.isload = true
            const params = {
                'itemId': _this.travelId,
                'size':_this.size,
                'page':_this.page - 1,
            }
            queryTravelItemPlan(params)
            .then(res=>{
                _this.isload = false
                if(res.success){
                    _this.dataList = res.data.content
                    _this.total = res.data.totalElements
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        // 添加
        add(){
            this.isAdd = true;
            this.item.id = '';
            this.item.name = '';
            this.item.content = '';
            this.item.imgUrl = '';
            this.item.itemId = this.travelId;
            this.imgList = [];
            this.visible = true
        },
        // 修改
        async update(item){
            let _this = this;
            _this.item = JSON.parse(JSON.stringify(item))
            this.isAdd = false;
            this.visible = true
        },
        // 确认添加、修改
        async handleOk(){
            let _this = this
            if(_this.item.name == ''){
                _this.$message.warning('请输入套餐名称')
                return;
            }
            if(_this.item.description == ''){
                _this.$message.warning('请输入套餐内容')
                return;
            }
            if(_this.item.amount == ''){
                _this.$message.warning('请输入套餐价格')
                return;
            }
            const params = {
                'itemId': _this.item.itemId,
                'id': _this.item.id,
                'name': _this.item.name,
                'description': _this.item.description,
                'amount': _this.item.amount,
                'origin': _this.item.origin,
                'destination': _this.item.destination,
                'number': _this.item.number,
            }
            updateTravelItemPlan(params)
            .then(async res=>{
                if(res.success){
                    _this.visible = false;
                    _this.getData();
                    _this.$notification['success']({
                        message: '提示',
                        description: _this.isAdd ? '添加成功' : '修改成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 删除
        del(item){
            let _this = this
            const params = {
                'itemId': item.itemId,
                'name': item.name,
                'id': item.id,
                'imdescriptiongUrl': item.description,
                'amount': item.amount,
                'origin': item.origin,
                'destination': item.destination,
                'isDelete': '1'
            }
            updateTravelItemPlan(params)
            .then(res=>{
                if(res.success){
                    _this.getData();
                    _this.$notification['success']({
                        message: '提示',
                        description: '删除成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 获取省市区
        async _queryCityList(){
            return new Promise((resolve,reject) => {
                queryTravelCity({})
                .then(res => {
                    if(res.success){
                        resolve(res.data)
                    }else{
                        reject(res.errMessage)
                    }
                })
            })
        },
        // 判断用户是否有权限
        hasAuth(role){
            // examineAdoptTrustOrderPaymentVoucher
            var roles = this.$store.getters.roles;
            console.log(roles.indexOf(role),'role')
            if(roles.indexOf(role) != -1){
                return true;
            }
            return false
        },
        async showItemDate(item){
            this.item = JSON.parse(JSON.stringify(item));
            this.planDateList = await this._queryTravelItemDate();
            this.planDateTrueFalseBy = true;
        },
        onClose(){
            this.planDateTrueFalseBy = false;
        },
        // 设置日期价格
        async dateEvent(value){
            console.log(value.format('YYYY-MM-DD'),'日期价格')
            this.planDateAmount = '';
            this.planDate = value.format('YYYY-MM-DD');
            let list = await this.showPlanDatePrice(value);
            console.log(list,'list');
            if(list.length > 0){
                this.planDateAmount = list[0];
                this.travelDay = list[1];
                this.planDateId = list[2];
            }else{
                this.planDateAmount = '';
                this.travelDay = '';
                this.planDateId = '';
            }
            this.planDatePriceVisible = true;
        },
        disabledDate(current){
            return current && current < moment().startOf('day');
        },
        async planDatePriceHandleOk(){
            let _this = this;
            if(_this.planDateAmount == ''){
                _this.$message.warning('请输入套餐在当前日期的价格')
                return;
            }
            const params = {
                'itemId': _this.travelId,
                'planId': _this.item.id,
                'date': _this.planDate,
                'amount': _this.planDateAmount,
                'travelDay': _this.travelDay,
                'id': _this.planDateId,
            }
            await _this._delTravelDate(params);
            _this.planDatePriceVisible = false;
        },
        // 查询项目套餐日期配置
        _queryTravelItemDate(){
            let _this = this;
            return new Promise((resolve,reject) => {
                const params = {
                    'itemId': _this.travelId,
                    'planId': _this.item.id,
                }
                queryTravelItemDate(params)
                .then(res => {
                    if(res.success){
                        let list = [];
                        let delList = [];
                        for (let index = 0; index < res.data.content.length; index++) {
                            const element = res.data.content[index];
                            if(moment(element.travelDate) < moment().startOf('day')){
                                // delList.push(element);
                                const params = {
                                    'itemId': _this.travelId,
                                    'planId': _this.item.id,
                                    'date': element.travelDate,
                                    'id': element.id,
                                    'isDelete': '1',
                                }
                                _this._delTravelDate(params,'1');
                            }else{
                                list.push(element);
                            }
                        }
                        resolve(list);
                    }else{
                        reject(res.errMessage);
                    }
                })
            })
        },
        // 删除日期
        async _delAllTravelDate(delList){
            for (let index = 0; index < delList.length; index++) {
                const element = delList[index];
                
            }
        },
        async _delTravelDate(params,noToast){
            let _this = this;
            updateTravelItemDate(params)
            .then(async res => {
                if(res.success){
                    if(!noToast){
                        _this.$notification['success']({
                            message: '提示',
                            description: '操作成功',
                            duration: 8,
                        })
                        _this.planDateList = await this._queryTravelItemDate();
                    }
                }else{
                    if(!noToast){
                        _this.$notification['error']({
                            message: '提示',
                            description: res.errMessage,
                            duration: 8,
                        })
                    }
                }
            })
        },
        showPlanDatePrice(value){
            let _this = this;
            let planItm;
            for (let index = 0; index < _this.planDateList.length; index++) {
                const element = _this.planDateList[index];
                if(element.travelDate == value.format('YYYY-MM-DD')){
                    planItm = element;
                    console.log(planItm,'planItm')
                }
            }
            let list = [];
            console.log('外层')
            if(planItm){
                list.push(planItm.amount);
                list.push(planItm.travelDay);
                list.push(planItm.id)
            }
            return list;
        },
    }
}

</script>
<style scoped>

</style>